package cn.tedu.myweibo.service.impl;

import cn.tedu.myweibo.exception.ServiceException;
import cn.tedu.myweibo.mapper.UserMapper;
import cn.tedu.myweibo.pojo.dto.UserLoginDTO;
import cn.tedu.myweibo.pojo.dto.UserRegDTO;
import cn.tedu.myweibo.pojo.entity.User;
import cn.tedu.myweibo.pojo.vo.UserVO;
import cn.tedu.myweibo.response.StatusCode;
import cn.tedu.myweibo.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserService implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public void reg(UserRegDTO userRegDTO) {
        System.out.printf("userRegDTO=" + userRegDTO);
        UserVO userVO = userMapper.selectByUsername(userRegDTO.getUsername());
        if (userVO!=null){//用户名存在
            //执行主业务过程中的任何异常情况都通过抛异常的方式解决
            throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        userMapper.insert(user);
    }

    @Override
    public void login(UserLoginDTO userLoginDTO) {

    }
}
